home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr27 / gs26.zip / PS2IMAGE.PS < prev    next >
Text File  |  1993-05-02  |  6KB  |  190 lines

  1. %    Copyright (C) 1990, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2. %    Distributed by Free Software Foundation, Inc.
  3. %
  4. % This file is part of Ghostscript.
  5. %
  6. % Ghostscript is distributed in the hope that it will be useful, but
  7. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. % to anyone for the consequences of using it or for whether it serves any
  9. % particular purpose or works at all, unless he says so in writing.  Refer
  10. % to the Ghostscript General Public License for full details.
  11. %
  12. % Everyone is granted permission to copy, modify and redistribute
  13. % Ghostscript, but only under the conditions described in the Ghostscript
  14. % General Public License.  A copy of this license is supposed to have been
  15. % given to you along with Ghostscript so you can know your rights and
  16. % responsibilities.  It should be in a file named COPYING.  Among other
  17. % things, the copyright notice and this notice must be preserved on all
  18. % copies.
  19.  
  20. % Convert a .ps file to another .ps file containing only a bit image.
  21. % After loading ps2image.ps into Ghostscript, invoke
  22. %    (output_file.ps) ps2image
  23. % This replaces the current device, writing the output on the file
  24. % instead of to the device.  Then invoke
  25. %    (your_input_file.ps) run
  26. % To display the image at a later time, simply run the file that was
  27. % written (output_file.ps).
  28.  
  29. % Initialize, and redefine copypage and showpage.
  30. /ps2idict 25 dict def
  31. ps2idict begin
  32.  
  33.         % Save the showpage operator.
  34.   /realshowpage /showpage load def
  35.  
  36.         % Define a monochrome palette.
  37.   /monopalette <00 ff> def
  38.  
  39.         % Initialize the tables for reading a image.
  40.         % This code gets copied to the output file.
  41.   /initread
  42.    {
  43.         % Initialize the strings for filling runs.
  44.  
  45.      /.ImageFills 31        % max repeat count
  46.      0 1 255
  47.       { 1 index string dup 0 1 5 index 1 sub { 3 index put dup } for
  48.         pop exch pop readonly exch
  49.       } for pop
  50.       256 array astore readonly def
  51.  
  52.         % Initialize the procedure table for input dispatching.
  53.  
  54.      /.ImageProcs
  55.      33 { { pop .ImageItem } } repeat
  56.      32 { {    % 0x21-0x40: (N-0x20) data bytes follow
  57.       32 sub 3 index exch 0 exch getinterval 2 index exch
  58.       readhexstring pop exch pop dup
  59.      } bind } repeat
  60.      31 { {    % 0x41-0x5f: repeat last data byte (N-0x40) times
  61.       64 sub .ImageFills 2 index dup length 1 sub get get
  62.       exch 0 exch getinterval
  63.      } bind } repeat
  64.      160 { { pop .ImageItem } } repeat
  65.      256 array astore readonly def
  66.  
  67.    } def
  68.         % Read one item from a compressed image.
  69.         % This procedure gets copied to the output file.
  70.         % Stack contents: <buffer> <file> <previous>
  71.   /.ImageItem
  72.    { 1 index read pop dup .ImageProcs exch get exec
  73.    } def
  74.         % Read and print an entire compressed image,
  75.         % scaling it uniformly in X and Y to fill the page.
  76.         % This procedure gets copied to the output file.
  77.         % Arguments: <width> <height>
  78.   /.ImageRead
  79.    { gsave 1 [
  80.      clippath pathbbox pop pop translate
  81.      pathbbox newpath 4 -2 roll pop pop
  82.      dup 3 1 roll abs 5 index exch div exch abs 6 index exch div max
  83.      0 0 2 index neg 0 4 index 7 -1 roll mul
  84.      ] { .ImageItem }
  85.      4 index 7 add 8 idiv string currentfile ()
  86.      8 3 roll
  87.      image pop pop pop
  88.      grestore showpage
  89.    } def
  90.  
  91.         % Write a repeat command on the file.
  92.   /writerepeat        % <count> writerepeat -
  93.    {  { dup 31 le { exit } if myfile (_) writestring 31 sub } loop
  94.      dup 0 ne { 64 add myfile exch write } { pop } ifelse
  95.    } bind def
  96.  
  97.         % Write a data command on the file.
  98.   /writedata        % <string> writedata -
  99.    {  { dup length 0 eq { exit } if
  100.     dup length 32 min
  101.     dup 16#20 add myfile exch write
  102.     1 index 0 2 index getinterval myfile exch writehexstring
  103.     myfile (\n) writestring
  104.     1 index length 1 index sub getinterval
  105.       }
  106.      loop pop
  107.    } bind def
  108.  
  109.         % Write an entire data string on the file.
  110.   /writedatastring        % <string> writedatastring -
  111.    {  { dup length 4 lt { exit } if
  112.         % Detect a maximal run of non-repeated data.
  113.     dup length 4 sub 0 exch 1 exch
  114.      { 2 copy get
  115.        dup 3 index 3 index 1 add get eq
  116.         { dup 3 index 3 index 2 add get eq
  117.            { dup 3 index 3 index 3 add get eq
  118.               { pop exit
  119.           } if
  120.            } if
  121.         } if
  122.       pop pop
  123.      }
  124.     for
  125.     1 add
  126.     1 index 0 2 index getinterval writedata
  127.     1 index length 1 index sub getinterval
  128.         % Detect a maximal run of repeated data.
  129.         % We know there are at least 3 repeated bytes.
  130.     dup 0 get exch
  131.     dup length exch 3 1 3 index 1 sub
  132.      {        % Stack: <byte> <length> <string> <index>
  133.        2 copy get 4 index ne { 3 -1 roll pop exch exit } { pop } ifelse
  134.      }
  135.     for        % Stack: <byte> <length> <string>
  136.     exch dup writerepeat
  137.     1 index length 1 index sub getinterval
  138.     exch pop
  139.       }
  140.      loop writedata
  141.    } bind def
  142.  
  143.         % The main procedure.
  144.   /ps2image
  145.    {                % Open the file
  146.      (w) file /myfile exch def
  147.      /initread load
  148.       { dup myfile exch write==only
  149.         type dup /arraytype eq exch /packedarraytype eq or
  150.      { (\n) } { ( ) } ifelse myfile exch writestring
  151.       } forall
  152.      { /.ImageItem /.ImageRead }
  153.       { dup myfile exch write==only
  154.         load myfile exch write==only
  155.     myfile ( bind def\n) writestring
  156.       } forall
  157.                     % Get the device parameters
  158.      currentdevice getdeviceprops dicttomark
  159.      dup /HWSize get aload pop
  160.        /devheight exch def
  161.        /devwidth exch def
  162.      /InitialMatrix get
  163.        /devmatrix exch def
  164.                 % Make a corresponding memory device
  165.      devmatrix devwidth devheight monopalette
  166.      makeimagedevice
  167.      /mydevice exch def
  168.      mydevice setdevice        % (does an erasepage)
  169.      /rowwidth devwidth 7 add 8 idiv def
  170.      /row rowwidth 7 add 8 idiv 8 mul string def    % pad for scanning
  171.                 % Replace the definition of showpage
  172.      userdict /showpage { ps2idict begin myshowpage end } bind put
  173.    } def
  174.                 % Write the image on the file
  175.   /myshowpage
  176.    { myfile devwidth write==only   myfile ( ) writestring
  177.      myfile devheight write==only   myfile ( .ImageRead\n) writestring
  178.                  % Write the hex data
  179.      0 1 devheight 1 sub
  180.       { mydevice exch row 0 rowwidth getinterval copyscanlines
  181.     writedatastring
  182.       } for
  183.      myfile flushfile
  184.      erasepage initgraphics
  185.    } bind def
  186.  
  187. end
  188.  
  189. /ps2image { ps2idict begin ps2image end } bind def
  190.